package model;

import java.util.List;

import javax.persistence.Query;

public class DAOAluno extends DAOJPA<Aluno>{
	
	public List<Turma> findTurmas(Aluno a){
		String login = a.getLogin();
		Query qr = getManager().createQuery("select t from Aluno a join a.turmas t where a.login=?");
		qr.setParameter(1, login);
		List<Turma> resultList = qr.getResultList();
		return resultList;
	}
	
	public List<Turma> findTurmasDisponiveis(Aluno a){
		String login = a.getLogin();
		String qr_str ="select distinct(t) from Aluno a, Turma t where ";
		qr_str+="t.capacidade >= size(t.alunos) and t not in "; 
		qr_str+="(select t1 from Aluno a1 join a1.turmas t1 where a1.login=?) ";
		qr_str+="order by t.curso,t.id";
		Query qr = getManager().createQuery(qr_str);
		qr.setParameter(1, login);
		List<Turma> resultList = qr.getResultList();
		return resultList;
	}
}
